<?php
require('connection.php');
set_include_path('./google-api-php-client/src');
// Call set_include_path() as needed to point to your client library.
require_once 'Google_Client.php';
require_once 'contrib/Google_YoutubeService.php';
session_start();

/* You can acquire an OAuth 2 ID/secret pair from the API Access tab on the Google APIs Console
  <http://code.google.com/apis/console#access>
For more information about using OAuth2 to access Google APIs, please visit:
  <https://developers.google.com/accounts/docs/OAuth2>
Please ensure that you have enabled the YouTube Data API for your project. */
$OAUTH2_CLIENT_ID = '686106885174-ljq8hnmm5m16tbh9hine70pifvevki3j.apps.googleusercontent.com';
$OAUTH2_CLIENT_SECRET = 'O0o14GL6Eq-4nyu0YCzLjW-6';

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);	
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
  FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

$youtube = new Google_YoutubeService($client);

if (isset($_GET['code'])) {
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.');
  }

  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: ' . $redirect);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
  try {
    $channelsResponse = $youtube->channels->listChannels('contentDetails', array(
      'mine' => 'true',
    ));
	print '<pre>';
	print_r($channel);
	print '</pre>';
    $htmlBody = '';
    foreach ($channelsResponse['items'] as $channel) {
      $uploadsListId = $channel['contentDetails']['relatedPlaylists']['uploads'];

      $playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
        'playlistId' => $uploadsListId,
        'maxResults' => 50
      ));
	$i = 1;
      foreach ($playlistItemsResponse['items'] as $playlistItem) {
			$htmlBody .=  "<div style='width:400px; float: left;'><h3>".$playlistItem['snippet']['title']."</h3>"."<h5>".$playlistItem['snippet']['description']."</h5><iframe width='300' height='200' src='//www.youtube.com/embed/".$playlistItem['snippet']['resourceId']['videoId']."' frameborder='0' allowfullscreen></iframe></div>";
		if($i++ % 2 == 0){
			$htmlBody .=  "<div sryle='clear: both;'>";
		}
	}
    }
  } catch (Google_ServiceException $e) {
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
      htmlspecialchars($e->getMessage()));
  } catch (Google_Exception $e) {
    $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
      htmlspecialchars($e->getMessage()));
  }

  $_SESSION['token'] = $client->getAccessToken();
} else {
  $state = mt_rand();
  $client->setState($state);
  $_SESSION['state'] = $state;

  $authUrl = $client->createAuthUrl();
  $htmlBody = <<<END
  <h3>Authorization Required</h3>
  <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
END;
}
echo $htmlBody; 
?>